package sgi.client;

import javax.swing.JFrame;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import javax.sql.rowset.*;
import com.sun.rowset.CachedRowSetImpl;

import java.util.*;
import java.util.Arrays;
//import javastat.util.BasicStatistics.*;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class WeeklyReportPanel extends JFrame {

	public WeeklyReportPanel(CachedRowSetImpl reportInfo) throws SQLException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		ArrayList<Integer> bought_images, bought_samples, new_images, new_samples;
		
		bought_images 	= new ArrayList<Integer>();
		bought_samples 	= new ArrayList<Integer>();
		new_images 		= new ArrayList<Integer>();
		new_samples		= new ArrayList<Integer>();
		
		while(reportInfo.next()) {
			bought_images.add((Integer.valueOf((reportInfo.getString(5)))));
			bought_samples.add((Integer.valueOf((reportInfo.getString(6)))));
			new_images.add((Integer.valueOf((reportInfo.getString(7)))));
		}
		
		while (bought_images.size()<7)
			bought_images.add( 0);
		while (bought_samples.size()<7)
			bought_samples.add( 0);
		while (new_images.size()<7)
			new_images.add( 0);
			
		
		Collections.sort(bought_images);
		Collections.sort(bought_samples);
		Collections.sort(new_images);
		
		double [] bis = new double [7];
		double [] bss = new double [7];
		double [] nis = new double [7];
		for (int i=0; i<7; i++){
			bis[i]=bought_images.get(i).doubleValue();
			bss[i]=bought_samples.get(i).doubleValue();
			nis[i]=new_images.get(i).doubleValue();
		}
		
		double median_bimages=median(bis);
		double median_bsamples=median(bss);
		double median_nimages=median(nis);
		
		double sd_bimages=standard_deviation(bis);
		double sd_bsamples=standard_deviation(bss);
		double sd_nimages=standard_deviation(nis);
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		setResizable(false);
		JLabel lblBoughtImagesInfo = new JLabel("Bought Images Info");
		GridBagConstraints gbc_lblBoughtImagesInfo = new GridBagConstraints();
		gbc_lblBoughtImagesInfo.insets = new Insets(0, 0, 5, 5);
		gbc_lblBoughtImagesInfo.gridx = 1;
		gbc_lblBoughtImagesInfo.gridy = 3;
		getContentPane().add(lblBoughtImagesInfo, gbc_lblBoughtImagesInfo);
		
		JLabel lblMedian = new JLabel("Median: ");
		GridBagConstraints gbc_lblMedian = new GridBagConstraints();
		gbc_lblMedian.insets = new Insets(0, 0, 5, 5);
		gbc_lblMedian.gridx = 2;
		gbc_lblMedian.gridy = 4;
		getContentPane().add(lblMedian, gbc_lblMedian);
		
		JLabel lblAmed = new JLabel("aMed");
		lblAmed.setText(Double.toString(median_bimages));
		GridBagConstraints gbc_lblAmed = new GridBagConstraints();
		gbc_lblAmed.insets = new Insets(0, 0, 5, 0);
		gbc_lblAmed.gridx = 4;
		gbc_lblAmed.gridy = 4;
		getContentPane().add(lblAmed, gbc_lblAmed);
		
		JLabel lblStandardDeviation = new JLabel("Standard Deviation: ");
		GridBagConstraints gbc_lblStandardDeviation = new GridBagConstraints();
		gbc_lblStandardDeviation.insets = new Insets(0, 0, 5, 5);
		gbc_lblStandardDeviation.gridx = 2;
		gbc_lblStandardDeviation.gridy = 5;
		getContentPane().add(lblStandardDeviation, gbc_lblStandardDeviation);
		
		JLabel lblAsd = new JLabel("aSD");
		lblAsd.setText(Double.toString(sd_bimages));
		GridBagConstraints gbc_lblAsd = new GridBagConstraints();
		gbc_lblAsd.insets = new Insets(0, 0, 5, 0);
		gbc_lblAsd.gridx = 4;
		gbc_lblAsd.gridy = 5;
		getContentPane().add(lblAsd, gbc_lblAsd);
		
		JLabel lblDistribution = new JLabel("Distribution: ");
		GridBagConstraints gbc_lblDistribution = new GridBagConstraints();
		gbc_lblDistribution.insets = new Insets(0, 0, 5, 5);
		gbc_lblDistribution.gridx = 2;
		gbc_lblDistribution.gridy = 6;
		getContentPane().add(lblDistribution, gbc_lblDistribution);
		
		JLabel lblAdist = new JLabel("aDist");
		lblAdist.setText(Arrays.toString(bis));
		GridBagConstraints gbc_lblAdist = new GridBagConstraints();
		gbc_lblAdist.insets = new Insets(0, 0, 5, 0);
		gbc_lblAdist.gridx = 4;
		gbc_lblAdist.gridy = 6;
		getContentPane().add(lblAdist, gbc_lblAdist);
		
		JLabel lblBoughtSamplesInfo = new JLabel("Bought Samples Info");
		GridBagConstraints gbc_lblBoughtSamplesInfo = new GridBagConstraints();
		gbc_lblBoughtSamplesInfo.insets = new Insets(0, 0, 5, 5);
		gbc_lblBoughtSamplesInfo.gridx = 1;
		gbc_lblBoughtSamplesInfo.gridy = 8;
		getContentPane().add(lblBoughtSamplesInfo, gbc_lblBoughtSamplesInfo);
		
		JLabel lblMedian_1 = new JLabel("Median: ");
		GridBagConstraints gbc_lblMedian_1 = new GridBagConstraints();
		gbc_lblMedian_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblMedian_1.gridx = 2;
		gbc_lblMedian_1.gridy = 9;
		getContentPane().add(lblMedian_1, gbc_lblMedian_1);
		
		JLabel lblAmed_1 = new JLabel("aMed");
		lblAmed_1.setText(Double.toString(median_bsamples));
		GridBagConstraints gbc_lblAmed_1 = new GridBagConstraints();
		gbc_lblAmed_1.insets = new Insets(0, 0, 5, 0);
		gbc_lblAmed_1.gridx = 4;
		gbc_lblAmed_1.gridy = 9;
		getContentPane().add(lblAmed_1, gbc_lblAmed_1);
		
		JLabel lblStandardDeviation_1 = new JLabel("Standard Deviation: ");	
		GridBagConstraints gbc_lblStandardDeviation_1 = new GridBagConstraints();
		gbc_lblStandardDeviation_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblStandardDeviation_1.gridx = 2;
		gbc_lblStandardDeviation_1.gridy = 10;
		getContentPane().add(lblStandardDeviation_1, gbc_lblStandardDeviation_1);
		
		JLabel lblAsd_1 = new JLabel("aSD");
		lblAsd_1.setText(Double.toString(sd_bsamples));
		GridBagConstraints gbc_lblAsd_1 = new GridBagConstraints();
		gbc_lblAsd_1.insets = new Insets(0, 0, 5, 0);
		gbc_lblAsd_1.gridx = 4;
		gbc_lblAsd_1.gridy = 10;
		getContentPane().add(lblAsd_1, gbc_lblAsd_1);
		
		JLabel lblDistribution_1 = new JLabel("Distribution: ");
		GridBagConstraints gbc_lblDistribution_1 = new GridBagConstraints();
		gbc_lblDistribution_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblDistribution_1.gridx = 2;
		gbc_lblDistribution_1.gridy = 11;
		getContentPane().add(lblDistribution_1, gbc_lblDistribution_1);
		
		JLabel lblAdist_1 = new JLabel("aDist");
		lblAdist_1.setText(Arrays.toString(bss));
		GridBagConstraints gbc_lblAdist_1 = new GridBagConstraints();
		gbc_lblAdist_1.insets = new Insets(0, 0, 5, 0);
		gbc_lblAdist_1.gridx = 4;
		gbc_lblAdist_1.gridy = 11;
		getContentPane().add(lblAdist_1, gbc_lblAdist_1);
		
		JLabel lblNewImagesInfo = new JLabel("New Images Info");
		GridBagConstraints gbc_lblNewImagesInfo = new GridBagConstraints();
		gbc_lblNewImagesInfo.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewImagesInfo.gridx = 1;
		gbc_lblNewImagesInfo.gridy = 13;
		getContentPane().add(lblNewImagesInfo, gbc_lblNewImagesInfo);
		
		JLabel lblMedian_2 = new JLabel("Median: ");
		GridBagConstraints gbc_lblMedian_2 = new GridBagConstraints();
		gbc_lblMedian_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblMedian_2.gridx = 2;
		gbc_lblMedian_2.gridy = 14;
		getContentPane().add(lblMedian_2, gbc_lblMedian_2);
		
		JLabel lblAmed_2 = new JLabel("aMed");
		lblAmed_2.setText(Double.toString(median_nimages));
		GridBagConstraints gbc_lblAmed_2 = new GridBagConstraints();
		gbc_lblAmed_2.insets = new Insets(0, 0, 5, 0);
		gbc_lblAmed_2.gridx = 4;
		gbc_lblAmed_2.gridy = 14;
		getContentPane().add(lblAmed_2, gbc_lblAmed_2);
		
		JLabel lblStandardDeviation_2 = new JLabel("Standard Deviation: ");
		GridBagConstraints gbc_lblStandardDeviation_2 = new GridBagConstraints();
		gbc_lblStandardDeviation_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblStandardDeviation_2.gridx = 2;
		gbc_lblStandardDeviation_2.gridy = 15;
		getContentPane().add(lblStandardDeviation_2, gbc_lblStandardDeviation_2);
		
		JLabel lblAsd_2 = new JLabel("aSD");
		lblAsd_2.setText(Double.toString(sd_nimages));
		GridBagConstraints gbc_lblAsd_2 = new GridBagConstraints();
		gbc_lblAsd_2.insets = new Insets(0, 0, 5, 0);
		gbc_lblAsd_2.gridx = 4;
		gbc_lblAsd_2.gridy = 15;
		getContentPane().add(lblAsd_2, gbc_lblAsd_2);
		
		JLabel lblDistribution_2 = new JLabel("Distribution");
		GridBagConstraints gbc_lblDistribution_2 = new GridBagConstraints();
		gbc_lblDistribution_2.insets = new Insets(0, 0, 0, 5);
		gbc_lblDistribution_2.gridx = 2;
		gbc_lblDistribution_2.gridy = 16;
		getContentPane().add(lblDistribution_2, gbc_lblDistribution_2);
		
		JLabel lblAdist_2 = new JLabel("aDist");
		lblAdist_2.setText(Arrays.toString(nis));
		GridBagConstraints gbc_lblAdist_2 = new GridBagConstraints();
		gbc_lblAdist_2.gridx = 4;
		gbc_lblAdist_2.gridy = 16;
		getContentPane().add(lblAdist_2, gbc_lblAdist_2);
		
		
		
		
		
	}

	
	private double median(double [] arr) {
		if (arr.length % 2 != 0)
			return arr[arr.length/2+1];
		else
			return (arr[arr.length/2]+arr[arr.length/2+1])/2;
	}
	
	private double standard_deviation(double [] arr) {
		
		double avg = 0;
		double sum = 0;
		
		double [] arr2 = new double [arr.length];
		
		for (int i=0; i<arr.length; i++)
			sum = sum + arr[i];
		avg = sum/arr.length;
		
		for (int i=0; i<arr.length; i++)
			arr2[i]=Math.pow(arr2[i]-avg, 2);
		
		double sum2=0;
		for (int i=0; i<arr.length; i++)
			sum2 = sum2 + arr2[i];
		
		return Math.sqrt(sum2/arr.length);
	}

}
